<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Database Input</title>
    <link rel="stylesheet" href="styles.css">
    <script src="send.js"></script>
</head>
<body>
    <form>
        <label for="dbname">数据库名称:</label><br>
        <input type="text" id="dbname" name="dbname"><br>
        <label for="filepath">路径:</label><br>
        <input type="text" id="filepath" name="filepath"><br>
        <p></p>
        <button type="button" onclick="createDatabase()">新建</button>
    </form>

    <script>
        function createDatabase() {
            //打开目录选择对话框，选择数据库要导出的文件夹
            const { dialog } = require('electron').remote;
            const path = dialog.showOpenDialogSync({ properties: ['openDirectory'] });
            if (path === undefined) {
                console.log('No path selected');
                return;
            }
            document.getElementById('filepath').value = path;
            const dbName = document.getElementById('dbname').value;
            const request = indexedDB.open('objass', 1);
            request.onupgradeneeded = (event) => {
                const db = event.target.result;
                if (!db.objectStoreNames.contains('objAssName')) {
                    // 创建一个名为objAssName的对象存储空间，含有id,name, content三个字段
                    const store = db.createObjectStore('objAssName', { keyPath: 'id', autoIncrement: true });
                    store.createIndex("name", "name", { unique: true });
                    store.put({ name: 'example1', content: 'This is an example file content1' });
                    store.put({ name: 'example2', content: 'This is an example file content2' });
                    // 创建一个名为objAssRelevant的对象存储空间，含有id, name1, name2, rel_1_2, rel_2_1, content这些字段
                    const store2 = db.createObjectStore('objAssRelevant', { keyPath: 'id', autoIncrement: true });
                    store2.put({ name1: 'example1', name2: 'example2', rel_1_2: '12', rel_2_1: '21', content: 'This is an example file content' });
                }
                console.log('Default data added to IndexedDB');
            };

            request.onsuccess = (event) => {
                const db = event.target.result;
                const transaction = db.transaction('files', 'readonly');
                const store = transaction.objectStore('files');
                const getRequest = store.getAll();

                getRequest.onsuccess = (event) => {
                    const files = event.target.result;
                    if (files.length > 0) {
                        files.forEach(file => {
                            const blob = new Blob([file.content], { type: 'text/plain' });
                            const link = document.createElement('a');
                            link.href = URL.createObjectURL(blob);
                            link.download = file.name;
                            link.click();
                        });
                        console.log('Files retrieved and downloaded');
                    } else {
                        console.error('No files found in IndexedDB');
                    }
                };

                getRequest.onerror = (event) => {
                    console.error('Retrieve error:', event.target.error);
                };
            };

            request.onerror = (event) => {
                console.error('IndexedDB error:', event.target.error);
            };
            sendMessageToParent('objass_main')
        }
    </script>
</body>
</html>
